﻿using System;
using ClientOverflow.Core;
using ClientOverflow.ViewModels.Interfaces;
using ClientOverflow.ViewModels.PostQuestion;

namespace ClientOverflow.ViewModels.MainMenus
{
    /// <summary>
    /// AskQuestionCommand sets the view to PostQuestion mode. Since posting a question requires a logged in user
    /// to be active, this command observes the ILoginService and changes its state according to the service it
    /// observes.
    /// </summary>
    public class AskQuestionCommand : INamedCommand
    {
        private readonly IMenusViewModel viewModel;
        private readonly ILoginService loginService;

        public AskQuestionCommand(IMenusViewModel viewModel)
        {
            this.viewModel = viewModel;
            loginService = ServicesProviderFactory.GetInstance().LoginService;
            loginService.OnLoginChanged += (sender, args) => CanExecuteChanged(this, EventArgs.Empty);
        }

        public string Name
        {
            get { return "Post question"; }
        }

        public void Execute(object parameter)
        {
            viewModel.Content = PostQuestionViewModel.CreatePostQuestionViewModel(new DataTransferObjects.Question());
        }

        public bool CanExecute(object parameter)
        {
            return loginService.LoggenInUser != null;
        }

        public event EventHandler CanExecuteChanged;
    }
}